#include "SkinPropertyManager.h"
#include <utility>

//#define PROPERTY_CODE(lpszProName,name,val) if (0==_tcscmp(lpszProName,#name)){return val;}
static SKIN_PROPERTY_TAG g_PropertyTag[] =
{
    {SKIN_PROPERTY_INDEX_IMAGE, _T( "image" ), SKIN_PROPERTY_STRING},
    {SKIN_PROPERTY_INDEX_VISIBLE, _T( "visible" ), SKIN_PROPERTY_BOOL},
    {SKIN_PROPERTY_INDEX_COLOR, _T( "color" ), SKIN_PROPERTY_COLOR},
    {SKIN_PROPERTY_INDEX_RECT, _T( "rect" ), SKIN_PROPERTY_RECT},
    {SKIN_PROPERTY_INDEX_FONT, _T( "font" ), SKIN_PROPERTY_FONT},
    {SKIN_PROPERTY_INDEX_INTLIST, _T( "intlist" ), SKIN_PROPERTY_INTLIST},
    {SKIN_PROPERTY_INDEX_POSITION, _T( "position" ), SKIN_PROPERTY_POSITION},
    {SKIN_PROPERTY_INDEX_FILE, _T( "file" ), SKIN_PROPERTY_STRING},
    {SKIN_PROPERTY_INDEX_MARGIN, _T( "margin" ), SKIN_PROPERTY_RECT},
    {SKIN_PROPERTY_INDEX_SIZE, _T( "size" ), SKIN_PROPERTY_SIZE},
    {SKIN_PROPERTY_INDEX_ID, _T( "id" ), SKIN_PROPERTY_INT}
};


CSkinPropertyManager::CSkinPropertyManager( void )
{

}

CSkinPropertyManager::~CSkinPropertyManager( void )
{

}

SKIN_PROPERTY_INDEX CSkinPropertyManager::GetPropertyIndex( LPCTSTR lpszPropertyName )
{
    SKIN_PROPERTY_INDEX uPropertyCode = SKIN_PROPERTY_INDEX_NULL;

    for ( int i = 0; i < ( sizeof( g_PropertyTag ) / sizeof( g_PropertyTag[0] ) ); i++ )
    {
        if ( 0 == _tcscmp( lpszPropertyName, g_PropertyTag[i].lpszProperName ) )
        {
            uPropertyCode = g_PropertyTag[i].skinPropertyIndex;
            break;
        }
    }

    return uPropertyCode;
}


SKIN_PROPERTY_TYPE CSkinPropertyManager::GetPropertyType( LPCTSTR lpszProName )
{
    for ( int i = 0; i < ( sizeof( g_PropertyTag ) / sizeof( g_PropertyTag[0] ) ); i++ )
    {
        if ( 0 == _tcscmp( lpszProName, g_PropertyTag[i].lpszProperName ) )
        {
            return g_PropertyTag[i].skinPropertyType;
        }
    }

    return SKIN_PROPERTY_UNKNOWN;
}

// void CSkinPropertyManager::AddProperty(UINT iCode,CSkinProperty* pSkinProperty)
// {
// 	m_mapProperty.insert(std::make_pair(iCode,pSkinProperty));
// }

CSkinProperty* CSkinPropertyManager::CreateProperty( LPCTSTR lpszProName, LPCTSTR lpszProValue )
{
    SKIN_PROPERTY_TYPE propertyType = GetPropertyType( lpszProName );

    if ( SKIN_PROPERTY_UNKNOWN == propertyType )
    {
        return NULL;
    }

    SKIN_PROPERTY_INDEX uPropertyCode = GetPropertyIndex( lpszProName );

    if ( SKIN_PROPERTY_INDEX_NULL == uPropertyCode )
    {
        return NULL;
    }

    CSkinProperty* pProperty = new CSkinProperty();

    switch ( propertyType )
    {
    case SKIN_PROPERTY_COLOR:
        pProperty->SetPropertyColor( lpszProValue );
        break;

    case SKIN_PROPERTY_STRING:
        pProperty->SetPropertyString( lpszProValue );
        break;

    case SKIN_PROPERTY_BOOL:
        pProperty->SetPropertyBool( lpszProValue );
        break;

    case SKIN_PROPERTY_RECT:
        pProperty->SetPropertyRect( lpszProValue );
        break;

    case SKIN_PROPERTY_INT:
        pProperty->SetPropertyInt( lpszProValue );
        break;

    case SKIN_PROPERTY_POSITION:
        pProperty->SetPropertySize( lpszProValue );
        break;

    case SKIN_PROPERTY_SIZE:
        pProperty->SetPropertySize( lpszProValue );
        break;

//	case SKIN_PROPERTY_ENUM:     pProperty->SetPropertyEnum(GetEnumCode(lpszProperty, lpszValue)); break;
    case SKIN_PROPERTY_FONT:
        pProperty->SetPropertyFont( lpszProValue );
        break;

    default:
        delete pProperty;
        return NULL;
    }

    pProperty->SetPropertyType( propertyType );
    pProperty->SetPropertyIndex( uPropertyCode );
    return pProperty;
}
